package com.weather.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.weather.common.utils.http.OkHttpUtils;
import com.weather.pojo.entity.Qweather;
import com.weather.pojo.entity.Weather;
import com.weather.service.QweatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class QweatherServiceImpl implements QweatherService {

    @Value("${qweather.key}")
    private String qweather_key;

    @Value("${qweather.keyHost}")
    private String key_host;

    private Map<String,String> Api_key=new HashMap<>();


    public List<Qweather> getQweatherList(String city) throws IOException {

        JSONObject jsonObject = getLookup(city);
        List<Qweather> result = new ArrayList<>();
        if ("200".equals(jsonObject.getString("code"))) {
            JSONArray locations = jsonObject.getJSONArray("location");
            for (int i = 0; i < locations.size(); i++) {
                JSONObject loc = locations.getJSONObject(i);
                Qweather qweather = new Qweather();
                qweather.setName(loc.getString("name"));
                qweather.setId(loc.getString("id"));
                qweather.setLat(loc.getString("lat"));
                qweather.setLon(loc.getString("lon"));
                qweather.setAdm2(loc.getString("adm2"));
                qweather.setAdm1(loc.getString("adm1"));
                qweather.setCountry(loc.getString("country"));
                qweather.setTz(loc.getString("tz"));
                qweather.setUtcOffset(loc.getString("utcOffset"));
                qweather.setIsDst(loc.getString("isDst"));
                qweather.setType(loc.getString("type"));
                qweather.setRank(loc.getString("rank"));
                qweather.setFxLink(loc.getString("fxLink"));
                result.add(qweather);
            }
        }
        log.info("方法getQweatherList");
        return result;
    }

    @Override
    public String getLocationId(String city) throws IOException {
        JSONObject jsonObject=getLookup(city);;
        if ("200".equals(jsonObject.getString("code"))) {
            JSONArray locations = jsonObject.getJSONArray("location");
            log.info("方法getLocationId,locations参数为:{}",locations.toString());
            if (locations != null && !locations.isEmpty()) {
                JSONObject first = locations.getJSONObject(1);
                log.info("方法getLocationId,入参数:{},返回值:{}",city,first.getString("id"));
                return first.getString("id");
            }
        }
        return null;
    }

    @Override
    public Weather getWeatherByLocationId(String locationId) throws IOException {
        Api_key.put("X-QW-Api-Key",qweather_key);
        Map<String,String> query=new HashMap<>();
        query.put("location",locationId);
        String s = OkHttpUtils.get("https://" + key_host + "/v7/weather/now",
                null, query, Api_key);
        JSONObject jsonObject = JSONObject.parseObject(s);
        if ("200".equals(jsonObject.getString("code"))) {
            JSONObject now = jsonObject.getJSONObject("now");
            if (now != null) {
                Weather weather = new Weather();
                weather.setObsTime(now.getString("obsTime"));
                weather.setTemp(now.getString("temp"));
                weather.setFeelsLike(now.getString("feelsLike"));
                weather.setIcon(now.getString("icon"));
                weather.setText(now.getString("text"));
                weather.setWind360(now.getString("wind360"));
                weather.setWindDir(now.getString("windDir"));
                weather.setWindScale(now.getString("windScale"));
                weather.setWindSpeed(now.getString("windSpeed"));
                weather.setHumidity(now.getString("humidity"));
                weather.setPrecip(now.getString("precip"));
                weather.setPressure(now.getString("pressure"));
                weather.setVis(now.getString("vis"));
                weather.setCloud(now.getString("cloud"));
                weather.setDew(now.getString("dew"));
                log.info("方法getWeatherByLocationId,入参数:{},返回值:{}",locationId,weather);
                return weather;
            }
        }
        return null;
    }

    private JSONObject getLookup(String city) throws IOException {
        Api_key.put("X-QW-Api-Key",qweather_key);
        Map<String,String> query=new HashMap<>();
        query.put("location",city);
        String string = OkHttpUtils.get(
                "https://"+key_host+"/geo/v2/city/lookup", null, query, Api_key);
        JSONObject jsonObject = JSONObject.parseObject(string);
        return jsonObject;
    }

}
